package com.hn658.user.core.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.user.common.entity.DepartmentEO;
import com.hn658.user.common.expection.DepartmentException;
import com.hn658.user.core.dao.db.IDepartmentDBDAO;
import com.hn658.user.core.service.IDepartmentService;
import com.hn658.user.sync.department.Department;
import com.hn658.user.sync.department.DepartmentHelper;
@Transactional(propagation=Propagation.SUPPORTS)
@Service
public class DepartmentServiceImpl extends AbstractBusinessObject implements IDepartmentService {

	@Autowired
	private IDepartmentDBDAO departmentDBDAO;
	
	@Override
	public List<DepartmentEO> getDirectChildrenDepts(String parentId, Boolean isDeleted) {
		if (StringUtils.isBlank(parentId)) {
			throw new DepartmentException(DepartmentException.EmptyParent);
		}
		DepartmentEO parent = departmentDBDAO.selectById(Long.parseLong(parentId));
		if (parent == null) {
			throw new DepartmentException(DepartmentException.EmptyParent);
		}

		Map<String, Object> query = new HashMap<String, Object>();

		query.put("parentDeptId", Long.parseLong(parentId.trim()));
		query.put("isDeleted", isDeleted);

		return departmentDBDAO.selectByMap(query);
	}

	@Override
	public DepartmentEO getDepartment(Long id) {
		if (id == null) {
			throw new DepartmentException(DepartmentException.IdEmptyWrong);
		}
		return departmentDBDAO.selectById(id);
	}
	
	@Override
	@Transactional
	public void addDepartment(DepartmentEO departmentEO) {
		DepartmentEO parent =  departmentDBDAO.selectById(departmentEO.getParentDeptId());
		if (parent == null) {
			throw new DepartmentException(DepartmentException.EmptyParent);
		}
		departmentEO.setIsDeleted(false);
		departmentEO.setCreateTime(new Date());
		departmentEO.setLastUpdateTime(new Date());
		long createDepartment = DepartmentHelper.createDepartment(departmentEO.getDeptName(), departmentEO.getParentDeptId().toString(), departmentEO.getDisplayOrder().toString(), true);
		
		if(createDepartment>0){
			departmentEO.setId(createDepartment);
			departmentDBDAO.insert(departmentEO);
		}
		
	}

	/**
	 * 部门不存在
	 * @param departmentEO
	 * @return
	 */
	private boolean hasDepartment(DepartmentEO departmentEO) {

        if(StringUtils.isBlank(departmentEO.getDeptName()) ) {
            throw new DepartmentException(DepartmentException.NameEmptyWrong);
        }
        if(departmentEO.getParentDeptId() == null) {
        	 throw new DepartmentException(DepartmentException.EmptyParent);
        }
        int count = departmentDBDAO.countDepartment(departmentEO);
        if (count > 0) {
        	if(count==1){
        		return true;
        	}else{
        		 throw new DepartmentException(DepartmentException.OneMoreWrong);
        	}
        }
        
        return false;
		
	}

	@Override
	@Transactional
	public void modifyDepartment(DepartmentEO departmentEO) {
		if(departmentEO==null){
			throw new DepartmentException(DepartmentException.EmptyWrong);
		}
		if(departmentEO.getId()==null){
			throw new DepartmentException(DepartmentException.IdEmptyWrong);
		}
		DepartmentEO dbDepartment = departmentDBDAO.selectById(departmentEO.getId());
		if(StringUtils.isNotBlank(departmentEO.getDeptName())
				&&!StringUtils.endsWith(departmentEO.getDeptName(), dbDepartment.getDeptName())){
			dbDepartment.setDeptName(departmentEO.getDeptName());
		}
		if(departmentEO.getParentDeptId()!=null
				&&departmentEO.getParentDeptId()!=dbDepartment.getParentDeptId()){
			dbDepartment.setParentDeptId(departmentEO.getParentDeptId());
		}
		if(StringUtils.isNotBlank(departmentEO.getDeptDesc())
				&&!StringUtils.endsWith(departmentEO.getDeptDesc(), dbDepartment.getDeptDesc())){
			dbDepartment.setDeptDesc(departmentEO.getDeptDesc());
		}
		if(departmentEO.getDisplayOrder()!=null
				&&departmentEO.getDisplayOrder()!=dbDepartment.getDisplayOrder()){
			dbDepartment.setDisplayOrder(departmentEO.getDisplayOrder());
		}
		dbDepartment.setIsDeleted(false);
		dbDepartment.setLastUpdateTime(new Date());
		
		Department department=new Department();
		department.setId(departmentEO.getId());
		department.setName(departmentEO.getDeptName());
		department.setOrder(department.getOrder());
		department.setParentid(departmentEO.getParentDeptId());
		DepartmentHelper.updateDepartment(department);
		departmentDBDAO.update(dbDepartment);
	}

	@Override
	@Transactional
	public void deleteDepartment(Long id) {
			if (id == null) {
				throw new DepartmentException(DepartmentException.IdEmptyWrong);
			}
			DepartmentEO departmentEO = departmentDBDAO.selectById(id);

			if (!this.hasDepartment(departmentEO)) {
				throw new DepartmentException(DepartmentException.NoOne);
			}

			departmentEO.setIsDeleted(Boolean.TRUE);
			departmentEO.setLastUpdateTime(new Date());
			 DepartmentHelper.deleteDepartment(id);
			 departmentDBDAO.update(departmentEO);
		}
	
}
